package com.hexin.download.util;

import com.hexin.download.service.impl.DownloadImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public final class FileUtils {
    private static Logger logger = LoggerFactory.getLogger(DownloadImpl.class);
    public static void logRecord(String info, String path){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path, true);
            fos.write(info.getBytes());
            fos.flush();
            fos.close();
        } catch (IOException e) {
            logger.error(info + "日志文件记录失败：exceptionMessage：{}",e.getMessage());
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    logger.error("文件流关闭失败：exceptionMessage：{}"+e.getMessage());
                }
            }
        }

    }
    public static String readJsonFile(String fileName) {
        String jsonStr;
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            logger.error("json文件传字符串失败：exceptionMessage：{}"+e.getMessage());
            return null;
        }
    }
}
